import {ClassicModel} from "../../models/Classic.js";
import {LikeModel} from "../../models/Like.js";

let classicModel = new ClassicModel();
let likeMode = new LikeModel();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    classic: null,
    latest: true,
    first: false,
    like:false,//是否点赞
    count:0,//点赞数
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    classicModel.getLatest((res) => {
      // console.dir(res);
      this.setData({
        classic: res,
        like:res.like_status,//是否点赞
        count:res.fav_nums,//点赞数
      });
    });
  },
  /**
   * 点赞
   */
  onLike: function (event) {
    let likeOrCanecel = event.detail.behavior;
    likeMode.like(likeOrCanecel, this.data.classic.id, this.data.classic.type);
  },
  onPre: function () {
    this._updateClassic("previous");
  },
  onNext: function () {
    this._updateClassic("next");
  },

  _updateClassic: function (previousOrNext) {
    let curruentIndex = this.data.classic.index;
    classicModel.getClassic(curruentIndex, previousOrNext, (res) => {
      this._getLikeStatus(res.id , res.type);
      this.setData({
        classic: res,
        latest: classicModel.isLatest(res.index),
        first: classicModel.isFirst(res.index),
      });
    });
  },

  _getLikeStatus:function(cid , type){
    likeMode.getClassicLikeStatus(cid , type ,(res)=>{
      this.setData({
        like:res.like_status,//是否点赞
        count:res.fav_nums,//点赞数
      });
    });
  }
})